﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.SLUnity;
using StopLight.ServiceImplementations;
using StopLight.Interfaces;
namespace StopLight.Logic {
    public class StoplightSchedule {
        private IStoplightTimer timer;
        private ILogger logger = new NullLogger();
        private TimeSpan[] lightTimes = new TimeSpan[3];
        private int currentLight = 0;

        public event EventHandler ChangeLight;

        public StoplightSchedule(IStoplightTimer timer) {
            this.timer = timer;
            timer.Expired += OnTimerExpired;
        }

        [Dependency]
        public ILogger Logger {
            get { return logger; }
            set { logger = value; }
        }

        public void Start() {
            timer.Start();
        }

        public void Update(TimeSpan green, TimeSpan yellow, TimeSpan red) {
            lightTimes[0] = green;
            lightTimes[1] = yellow;
            lightTimes[2] = red;

            logger.Write(string.Format("UPDATE SCHEDULE: {0} {1} {2}", green, yellow, red));
        }

        public void ForceChange() {
            OnTimerExpired(this, EventArgs.Empty);
            logger.Write(string.Format("FORCED CHANGE"));
        }

        private void OnTimerExpired(object sender, EventArgs e) {
            EventHandler handlers = ChangeLight;
            if (handlers != null) {
                handlers(this, EventArgs.Empty);
            }
            currentLight = (currentLight + 1) % 3;
            timer.Duration = lightTimes[currentLight];
            timer.Start();
        }
    }
}
